package com.bac.rentmap.ad.flat.rating.item;

import com.bac.rentmap.ad.flat.FlatAd;

public class UserReviewRatingItemCalculator implements RatingItemCalculator {
	private static final int MAX_POSSIBLE_RATING = 0;

	public long calculate(FlatAd flatAd) {
		long res = calculateUserReviewRating(flatAd.getAgentUserReviewCount());
		res += calculateUserReviewRating(flatAd.getBadPhoneUserReviewCount());
		res += calculateUserReviewRating(flatAd.getExpiredAdUserReviewCount());
		return res;
	}

	public static final int MAX_VALUABLE_USER_REVIEW_COUNT = 10;
	private static final int MIN_USER_REVIEW_CORRECTION = -10;
	private static final int USER_REVIEW_RATING_RATIO = 2;

	private long calculateUserReviewRating(Integer count) {
		if (count == null || count == 0) {
			return MAX_POSSIBLE_RATING;
		}
		if (count > MAX_VALUABLE_USER_REVIEW_COUNT) {
			count = MAX_VALUABLE_USER_REVIEW_COUNT;
		}
		return MIN_USER_REVIEW_CORRECTION - count * USER_REVIEW_RATING_RATIO;
	}

	public long getMaxPossibleRating() {
		return MAX_POSSIBLE_RATING;
	}

	/**
	 * Rating for 10 bad user reviews
	 */
	public long getMinPossibleRating() {
		return MIN_USER_REVIEW_CORRECTION - 10 * USER_REVIEW_RATING_RATIO;
	}
}
